package team.seekers.catering.food.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.seekers.catering.food.entity.dao.Combo;
import team.seekers.catering.food.entity.dao.Product;
import team.seekers.catering.food.entity.dto.add.ProductAddDTO;
import team.seekers.catering.food.entity.dto.update.ProductUpdateDTO;
import team.seekers.catering.food.entity.pojo.ContainLabelListDO;
import team.seekers.catering.food.entity.vo.info.ProductInfoVO;
import team.seekers.catering.food.entity.vo.list.ProductListVO;
import team.seekers.catering.food.mapper.ProductMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductService extends ServiceImpl<ProductMapper, Product> {

    @Autowired
    private ComboService comboService;


    /**
     * 新增商品
     */
    public String add(ProductAddDTO productAddDTO){
        Product product = new Product();
        BeanUtils.copyProperties(productAddDTO, product);
        save(product);
        return product.getId();
    }
    /**
     * 修改商品
     */
    public String update(ProductUpdateDTO productUpdateDTO) {
        Product product = new Product();
        BeanUtils.copyProperties(productUpdateDTO, product);
        updateById(product);
        return product.getId();
    }
    /**
     * 根据id得到商品详情
     */
    public ProductInfoVO get(String id) {
        Product product = getById(id);
        return goodsToVO(product);
    }
    /**
     * 根据套餐返回套餐信息
     */
    public ProductInfoVO goodsToVO(Product product){
        ProductInfoVO productInfoVO = new ProductInfoVO();
        BeanUtils.copyProperties(product, productInfoVO);
        Combo combo = comboService.getById(product.getComboId());

        productInfoVO.setDescription(combo.getDescription());
        productInfoVO.setPicture(combo.getPicture());

        List<ContainLabelListDO> label = JSONObject.parseArray(combo.getLabel(), ContainLabelListDO.class);
        productInfoVO.setLabel(label);

        return productInfoVO;
    }

    /**
     * 分页查询所有套餐
     */
    public Map<String, Object> adminPageGoods(long current, long limit, QueryWrapper<Product> wrapper) {
        Page<Product> page = new Page<>(current,limit);
        wrapper.orderByDesc("gmt_create");
        page(page,wrapper);
        Long total = page.getTotal();
        if(page.getRecords() == null){
            return null;
        }
        List<ProductListVO> list = new ArrayList<>();
        for (Product record : page.getRecords()) {
            ProductListVO vo = new ProductListVO();
            BeanUtils.copyProperties(record,vo);
            Combo combo = comboService.getById(record.getComboId());
            vo.setDescription(combo.getDescription());
            vo.setPicture(combo.getPicture());
            list.add(vo);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",list);
        return map;
    }

}
